<?php


namespace App\Controller;


use Common\Model\MemberModel;
use SidToken\Model\QueueModel;
use SidToken\Model\SidTransferLogModel;
use SidToken\Utils\MemberWalletUtil;
use SidToken\Utils\TrxRpc;

class NotifyController extends BaseController
{
    protected function _initialize()
    {
        $whiteListConfig = C("WHITELIST");
        $crontabWhitelist = $whiteListConfig['crontab'];
        if (!in_array(getRealIp(), $crontabWhitelist)) {
            exit("permission deny");
        }
    }

    /**
     * 用户账户收到转账回调
     */
    public function deposit()
    {
        notify_log("deposit ========================");
        notify_log(json_encode($_REQUEST));
        notify_log("deposit end ========================");

        /**
         * {
         * "block": "29180649",
         * "coinType": "TRC20",
         * "fromAddress": "TZArAEnhFiUnfxskhCBgiXTT8PfY9iFNYT",
         * "hash": "1aee9611f960c9b879a1fa547133b4c0b4ff6dd56834ec55d3eb597df853159f",
         * "sign": "44299e396a8dedadc0feb36ca8e03168",
         * "status": "S",
         * "time": "2021-04-09T20:20:59.046",
         * "toAddress": "TDD5ovyyeJS3WF6EPGdrot6yJ4Dudp5vGq",
         * "txid": "1aee9611f960c9b879a1fa547133b4c0b4ff6dd56834ec55d3eb597df853159f",
         * "type": "1",
         * "usdtAmount": "0.200000000000000000"
         * }
         */
        $deposit = $_REQUEST;
        if ($deposit['status'] == 'F') {
            $this->jsuccess();
        }
        /**
         * 0是转出成功
         * 1 是转入成功
         * 2是转出失败
         * 3 是转入失败
         */
        if (intval($deposit['type']) != 1) {
            $this->jsuccess();
        }
        $WalletUtil = new MemberWalletUtil();
        $uid = get_uid_by_address($deposit['toAddress']);
        if (!$uid) {
            $this->jerror("uid not exist");
        }
        $TransLogModel = new SidTransferLogModel();
        if ($TransLogModel->isTxidExist($deposit['txid'])) {
            $this->jerror("txid exist");
        }
        if ($deposit['coinType'] == "TRC20") {
            $WalletUtil->changeSid($uid, 'inc', $deposit['fromAddress'], $deposit['toAddress'], $deposit['txid'], floatval($deposit['usdtAmount']), 'SID转账');
            //$this->toMainAccountTask($uid, $deposit['usdtAmount'], 'SID');
        } else if ($deposit['coinType'] == "USD") {
            $WalletUtil->changeUsdt($uid, 'inc', $deposit['fromAddress'], $deposit['toAddress'], $deposit['txid'], floatval($deposit['usdtAmount']), 'USDT转账');
            //$this->toMainAccountTask($uid, $deposit['usdtAmount'], 'USDT');
        }
        $this->jsuccess('', 'success');
    }

}